<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link href="./bootstrap/css/bootstrap.css" rel="stylesheet" type="text/css"/>
    <script src="https://s3.pstatp.com/cdn/expire-1-M/jquery/3.1.1/jquery.min.js"></script>
    <script src="./bootstrap/js/bootstrap.js"></script>
    <script> function recoverPassword() {
        alert("请前往您的邮箱或联系管理员找回密码！");
    }
    </script>
    <style>
        .block-content {
            display: flex;
            flex-wrap: wrap;
        }

        .goods-list {
            width: 25%;
            display: flex;
            justify-content: center;
            margin-bottom: 20px;
        }

        .goods-block {
            width: 260px;
            padding: 0 30px;
        }

        .goods-img {
            width: 200px;
            height: 200px;
        }

        .foot {
            display: flex;
            align-items: center;
            justify-content: space-between;
        }

        .add-card {
            width: 30px;
            height: 30px;
        }

        .mt20 {
            margin-top: 20px;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">简单商城</a>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav navbar-right">
                <input type="button" class="loginbtn" value="找回密码" onclick="recoverPassword()" />
                <li><a href="a" data-toggle="modal" data-target="#consumerRegisterModal">注册</a></li>
                <li><a href="a" data-toggle="modal" data-target="#consumerLoginModal">登录</a></li>

                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">退出系统 <span class="caret"></span></a>
                    <ul class="dropdown-menu">
                        <li><a href="login.html">安全退出</a></li>
                    </ul>
                </li>
            </ul>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<div class="block-content">
    <div class="goods-list">
        <div class="goods-block">
            <img src="./images/1.png"
                 alt="" class="goods-img">
            <div class="foot mt20">
                <div class="goods-name">名称</div>
                <img src="./images/cart.png"
                     alt="" class="add-card">
            </div>
            <div class="foot mt20">
                <div class="new-price">30.60元</div>
                <div class="old-price">50.00元</div>
            </div>
        </div>
    </div>

    <div class="goods-list">
        <div class="goods-block">
            <img src="./images/2.png"
                 alt="" class="goods-img">
            <div class="foot mt20">
                <div class="goods-name">名称</div>
                <img src="./images/cart.png"
                     alt="" class="add-card">
            </div>
            <div class="foot mt20">
                <div class="new-price">30.60元</div>
                <div class="old-price">50.00元</div>
            </div>
        </div>
    </div>

    <div class="goods-list">
        <div class="goods-block">
            <img src="./images/3.png"
                 alt="" class="goods-img">
            <div class="foot mt20">
                <div class="goods-name">名称</div>
                <img src="./images/cart.png"
                     alt="" class="add-card">
            </div>
            <div class="foot mt20">
                <div class="new-price">30.60元</div>
                <div class="old-price">50.00元</div>
            </div>
        </div>
    </div>

    <div class="goods-list">
        <div class="goods-block">
            <img src="./images/4.png"
                 alt="" class="goods-img">
            <div class="foot mt20">
                <div class="goods-name">名称</div>
                <img src="./images/cart.png"
                     alt="" class="add-card">
            </div>
            <div class="foot mt20">
                <div class="new-price">30.60元</div>
                <div class="old-price">50.00元</div>
            </div>
        </div>
    </div>

    <div class="goods-list">
        <div class="goods-block">
            <img src="./images/5.png"
                 alt="" class="goods-img">
            <div class="foot mt20">
                <div class="goods-name">名称</div>
                <img src="./images/cart.png"
                     alt="" class="add-card">
            </div>
            <div class="foot mt20">
                <div class="new-price">30.60元</div>
                <div class="old-price">50.00元</div>
            </div>
        </div>
    </div>
</div>




<div class="modal fade" id="consumerRegisterModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabelReg">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabel">用户注册</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal">
                    <div class="form-group">
                        <label for="inputAccount" class="col-sm-2 control-label">账号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="inputAccount" placeholder="请输入登录账号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="inputPassword" placeholder="请输入密码">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPasswordConf" class="col-sm-2 control-label">确认密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="inputPasswordConf" placeholder="请确认密码">
                        </div>
                    </div>
                    <span style="color:red" id="tipmessReg"></span>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="consumerReg()">注册</button>
            </div>
        </div>
    </div>
</div>

<div class="modal fade" id="consumerLoginModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabelLogin">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="myModalLabelLogin">用户登录</h4>
            </div>
            <div class="modal-body">

                <form class="form-horizontal">
                    <div class="form-group">
                        <label for="inputAccountLogin" class="col-sm-2 control-label">账号</label>
                        <div class="col-sm-10">
                            <input type="text" class="form-control" id="inputAccountLogin" placeholder="请输入登录账号">
                        </div>
                    </div>
                    <div class="form-group">
                        <label for="inputPassword" class="col-sm-2 control-label">密码</label>
                        <div class="col-sm-10">
                            <input type="password" class="form-control" id="inputPasswordLogin" placeholder="请输入密码">
                        </div>
                    </div>
                    <span style="color:red" id="tipmessLogin"></span>
                </form>

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" onclick="consumerLogin()">登录</button>
            </div>
        </div>
    </div>
</div>



<script language="javascript">
    $(function(){
        $('.loginbox').css({'position':'absolute','left':($(window).width()-692)/2});
        $(window).resize(function(){
            $('.loginbox').css({'position':'absolute','left':($(window).width()-692)/2});
        })
    });
    // 商家注册按钮点击时执行的函数，用来判断商家填写账号和密码了没有
    function consumerReg(){
        // 获取账号信息

        let account = document.getElementById('inputAccount').value;
        let passwd = document.getElementById('inputPassword').value;
        let passwordConf = document.getElementById('inputPasswordConf').value;


        // 判断账号密码都不为空
        if(account == '' || passwd == '' || passwordConf == ''){
            // 输出错误信息
            document.getElementById('tipmessReg').innerHTML = '请把账号密码填写完整';
            // 有空值就结束执行
            return;
        } else {
            if (passwd != passwordConf){
                document.getElementById('tipmessReg').innerHTML = '两次密码不一致';
                return;
            }else {
                document.getElementById('tipmessReg').innerHTML = '';
                // 判断账号是否存在，存在则不能注册，反之可以注册
                // 发送ajax请求，判断账号是否已经存在


                let bj = {name:account,password: passwd};
                $.ajax({
                    url: 'consumer/add',        // 这是后台消费者注册地址
                    type: 'post',               // 后台接口的请求方式
                    data: JSON.stringify(bj),  // JSON对象转JSON字符串
                    dataType:'json',            // 后端给前端返回的数据格式
                    contentType: 'application/json',// 前端给后端的返回数据格式
                    success: function (data){
                        if (data.code == 200) {
                            // 注册成功可以登录
                            document.getElementById("tipmessReg").innerHTML = '注册成功，请登录';
                            return;
                        }else {
                            // 账号存在不能注册
                            document.getElementById("tipmessReg").innerHTML = '账号已存在请重新注册';
                            return;
                        }
                    }
                })
            }
        }

    }

    //商家注册完毕需要登陆的函数
    function consumerLogin(){
        let account = document.getElementById('inputAccountLogin').value;
        let passwd = document.getElementById('inputPasswordLogin').value;
        if (account == '' || passwd == ''){
            document.getElementById("tipmessLogin").innerHTML = '账号和密码请填写完整';
            return;
        }else {
            // 账号和密码不能为空，判断数据库中是否真的存在这里的账号和密码
            // 发送ajax请求，判断账号是否已经存在
            let bj = {name:account,password: passwd};
            $.ajax({
                url: 'consumer/login',        // 这是后台商家注册地址
                type: 'post',               // 后台接口的请求方式
                data: JSON.stringify(bj),  // JSON对象转JSON字符串
                dataType: 'json',           //后端给前端返回的数据格式
                contentType: 'application/json',//前端给后端返回的数据格式
                success: function (data) {
                    if (data.code == 200){
                        // 登录成功，界面要跳转到系统内部的主页
                        window.location.href = 'client.html'
                    }else {
                        document.getElementById("tipmessLogin").innerHTML = '账号和密码不正确，请核实……';
                        return;
                    }
                }
            })
        }
    }
</script>



</body>
</html>